package com.hualian.user.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hualian.system.common.Constants;
import com.hualian.user.controller.TeamController;
import com.hualian.user.mapper.TeamMapper;
import com.hualian.user.mapper.UserMapper;

@Service
public class TeamService {

	@Autowired
	private TeamMapper teamMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 根据参数查询队伍信息
	 * @param params
	 * @return
	 */
	public List<Map<String, Object>> getTeam(Map<String,Object> params){
		return teamMapper.getTeam(params);
	}
	
	
	/**
	 * 增加队伍
	 * @param params
	 * @return
	 */
	public int addTeam(Map<String,Object> params,Integer userId){
		//新增队伍
		int num = teamMapper.addTeam(params);
		//获得新建的队伍的主键
		int teamId = Integer.valueOf(params.get("id").toString());
		
		Map<String,Object> userParams = new HashMap<String, Object>();
		userParams.put("id", userId);
		userParams.put("teamId", teamId);
		userParams.put("isCaptain", Constants.USER_IS_CAPTAIN);
		//更新用户的队伍主键,并将用户设置为队长
		userMapper.updateUser(userParams);
		
		Map<String,Object> user2teamParams = new HashMap<String, Object>();
		user2teamParams.put("userId", userId);
		user2teamParams.put("teamId", teamId);
		//0表示加入队伍
		user2teamParams.put("operate", Constants.USER_2_TEAM_JOIN);
		//记录用户加入队伍
		userMapper.addUser2team(user2teamParams);
		
		return num;
	}
	
	
	/**
	 * 修改队伍信息
	 * @param params
	 * @return
	 */
	public int updateTeam(Map<String,Object> params){
		return teamMapper.updateTeam(params);
	}
	
	
	/**
	 * 删除队伍
	 * @param params
	 * @return
	 */
	public int removeTeam(Map<String,Object> params){
		return teamMapper.removeTeam(params);
	}
	
	
	/**
	 * 队伍增加成员
	 * @param params
	 * @return
	 */
	public int addMember(Map<String,Object> params,int teamMaxQuantity){
		//传入的参数  userId:用户表主键,teamId:队伍表主键
		
		
		
		Map<String,Object> userParams = new HashMap<String, Object>();
		userParams.put("id", params.get("userId"));
		userParams.put("teamId", params.get("teamId"));
		
		Map<String,Object> teamParams = new HashMap<String, Object>();
		teamParams.put("id", params.get("teamId"));
		
		Map<String,Object> user2teamParams = new HashMap<String, Object>();
		user2teamParams.put("userId", params.get("userId"));
		user2teamParams.put("teamId", params.get("teamId"));
		//0表示加入队伍
		user2teamParams.put("operate", Constants.USER_2_TEAM_JOIN);
		
		
		List<Map<String,Object>> teamList = teamMapper.getTeam(teamParams);
		if(teamList.size()==1){
			int quantity = Integer.valueOf(teamList.get(0).get("quantity").toString());
			// 如果人数已经满了,则不能增加成员
			if(quantity >= teamMaxQuantity){
				return Constants.TEAM_IS_FULL;
			}
		}else{
			return Constants.TEAM_NOT_EXISTS;
		}
		
		//将用户表中的队伍主键设置为要加入的队伍主键
		int num = userMapper.updateUser(userParams);
		
		//增加成员后修改队伍人数
		teamMapper.addMember(teamParams);
		
		
		//记录用户加入队伍
		userMapper.addUser2team(user2teamParams);
		
		return num;
	}
	
	
	/**
	 * 传入的参数  userId:用户表主键,teamId:队伍表主键
	 * 队伍移除成员
	 * @param params
	 * @return
	 */
	public int removeMember(Map<String,Object> params,Map<String,Object> teamInfo){
		Map<String,Object> userParams = new HashMap<String, Object>();
		userParams.put("id", params.get("userId"));
		
		//TODO 想一个清空 队伍主键的方法
		userParams.put("teamId", Constants.USER_NOT_JOIN_TEAM);
		//由于退出了队伍,清空队长标志
		userParams.put("isCaptain", Constants.USER_NOT_CAPTAIN);
		
		
		Map<String,Object> teamParams = new HashMap<String, Object>();
		teamParams.put("id", params.get("teamId"));
		//设置标记 队伍删除
		teamParams.put("isDelete", Constants.IS_DELETE);
		
		Map<String,Object> user2teamParams = new HashMap<String, Object>();
		user2teamParams.put("userId", params.get("userId"));
		user2teamParams.put("teamId", params.get("teamId"));
		//1表示离开队伍
		user2teamParams.put("operate", Constants.USER_2_TEAM_LEAVE);
		
		
		//将用户表中的队伍主键清空
		int num = userMapper.updateUser(userParams);
		
		//如果队伍人数只剩下一人(即队长),则删除队伍
		if(Integer.valueOf(teamInfo.get("quantity").toString())==1){
			teamMapper.updateTeam(teamParams);
		}
		
		//移除成员后修改队伍人数
		teamMapper.removeMember(teamParams);
		
		//记录用户离开队伍
		userMapper.addUser2team(user2teamParams);
		
		return num;
	}
	
	/**
	 * 委任队长
	 * 传进来的参数:  userId:登陆地用户主键,  newCaptainId:新队长主键,  teamId:队伍主键
	 * @param params
	 * @return
	 */
	public Integer appointCaptain(Map<String,Object> params){
		//本人主键
		Integer userId = Integer.valueOf(params.get("userId").toString());
		//新队长主键
		Integer newCaptainId = Integer.valueOf(params.get("newCaptainId").toString());
		//本队伍主键
		Integer teamId = Integer.valueOf(params.get("teamId").toString());
		
		Map<String,Object> userParams = new HashMap<String, Object>();
		userParams.put("id", userId);
		userParams.put("isCaptain", Constants.USER_NOT_CAPTAIN);
		//将本人设置成非队长
		userMapper.updateUser(userParams);
		
		Map<String,Object> newUserParams = new HashMap<String, Object>();
		newUserParams.put("id", newCaptainId);
		newUserParams.put("isCaptain", Constants.USER_IS_CAPTAIN);
		//将将要委任的用户设置为队长
		userMapper.updateUser(newUserParams);
		
		Map<String,Object> teamParams = new HashMap<String, Object>();
		teamParams.put("id", teamId);
		teamParams.put("captainId",newCaptainId );
		//将队伍队长主键设置为新队长主键
		int rows = teamMapper.updateTeam(teamParams);
		
		return rows;
	}
	
	
}
